package com.toby.ioc.config;

import com.toby.ioc.beanlifecycle.LifeCycleBean1;
import com.toby.ioc.component.TobyBean;
import com.toby.ioc.component.TobyDao;
import com.toby.ioc.component.TobyService;
import com.toby.ioc.conditional.TobyA;
import com.toby.ioc.conditional.TobyB;
import com.toby.ioc.conditional.TobyCondition;
import com.toby.ioc.filter.TobyTypeFilter;
import com.toby.ioc.imports.TobyImportBeanDefinitionRegistrar;
import com.toby.ioc.imports.TobyImportSelector;
import com.toby.ioc.processor.TobyBeanPostProcessor;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

/**
 * @desc: ioc config 类
 * @author: toby
 * @date: 2019/7/13 1:10
 */
@Configuration
@ComponentScan(basePackages = {"com.toby.ioc"})
//1、排除用法
/*@ComponentScan(basePackages = {"com.toby.ioc"},excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class}),
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = {TobyService.class})
})*/
//2、包含用法 includeFilters ,注意，若使用包含的用法，需要把useDefaultFilters属性设置为false（true表示扫描全部的）
/*@ComponentScan(basePackages = {"com.toby.ioc"},includeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class, Service.class})
},useDefaultFilters = false)*/
//3、自定义Filter
/*@ComponentScan(basePackages = {"com.toby.ioc"},includeFilters = {
        @ComponentScan.Filter(type = FilterType.CUSTOM,value = TobyTypeFilter.class)
},useDefaultFilters = false)*/
@Import({TobyBean.class, TobyImportSelector.class, TobyImportBeanDefinitionRegistrar.class})
public class IocConfig {

    @Bean(initMethod = "init",destroyMethod = "destroy",autowire = Autowire.BY_TYPE)
    public LifeCycleBean1 lifeCycleBean1(){
        return new LifeCycleBean1();
    }

    @Bean
    public TobyA tobyA(){
        return new TobyA();
    }

    @Bean
    @Conditional(TobyCondition.class)
    public TobyB tobyB(){
        return new TobyB();
    }

    @Bean
    @Primary
    public TobyDao tobyDao(){
        return new TobyDao();
    }

    @Bean
    public TobyDao tobyDao2(){
        return new TobyDao();
    }

    @Bean
    public TobyBeanPostProcessor tobyBeanPostProcessor(){
        return new TobyBeanPostProcessor();
    }

}
